package models

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"mange/appconfig"
)

var DB *gorm.DB
var err error

func Init() {
	// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
	data := appconfig.Conf.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/mange_name?charset=utf8mb4&parseTime=True&loc=Local", data.User, data.Password, data.Host, data.Port)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("连接失败")
	} else {
		fmt.Println("连接成功")
	}
	err = DB.AutoMigrate(&User{}, &Mange{})
	if err != nil {
		panic("迁移失败")
	} else {
		fmt.Println("迁移成功")
	}
}
